package nl.rainbowsheep.reisorg.empl.site.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.rainbowsheep.reisorg.empl.site.model.City;
import nl.rainbowsheep.reisorg.empl.site.model.Country;
import nl.rainbowsheep.reisorg.empl.site.model.Region;
import nl.rainbowsheep.reisorg.empl.site.model.Trip;
import nl.rainbowsheep.reisorg.empl.site.service.CityService;
import nl.rainbowsheep.reisorg.empl.site.service.CountryService;
import nl.rainbowsheep.reisorg.empl.site.service.RegionService;
import nl.rainbowsheep.reisorg.empl.site.service.TripService;
import nl.rainbowsheep.reisorg.empl.site.viewmodel.TripViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/trip")
public class TripController {

    @Autowired
    TripService tripservice;
    @Autowired
    CountryService countryService;
    @Autowired
    RegionService regionService;
    @Autowired
    CityService cityService;
    
    
    @RequestMapping(value = {"/", "/index", ""})
    public ModelAndView getAll() {
        List<TripViewmodel> trips = tripservice.getAll();
        ModelAndView model = new ModelAndView("trip/index");
        model.addObject("trips", trips);
        return model;
    }

    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public ModelAndView getTripDetails(@PathVariable long id) {
        TripViewmodel trip = tripservice.getById(id);
        ModelAndView mav = new ModelAndView("trip/details");
        mav.addObject("tripviewmodel", trip);
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView getTripForm() {
        ObjectMapper mapper = new ObjectMapper();
        ModelAndView mav = new ModelAndView("trip/create");
        mav.addObject("tripviewmodel", new TripViewmodel());
        List<Country> countries = countryService.getAll();
        List<Region> regions = regionService.getAll();
        List<City> cities = cityService.getAll();
        try {
            mav.addObject("countries", mapper.writeValueAsString(countries));
            mav.addObject("regions", mapper.writeValueAsString(regions));
            mav.addObject("cities", mapper.writeValueAsString(cities));
        } catch (JsonProcessingException ex) {
            Logger.getLogger(TripController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mav;
    }

    /**
     * Method for saving a newly created Trip object.
     *
     * @param trip
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody Trip createTripViewmodel(@RequestBody Trip trip) {
//        if (bindingResult.hasErrors()) {
//            return model;
//        }
        return tripservice.save(trip);
        
        //return new ModelAndView("trip/create");
    }
}
